package com.lxlx.weixinlx.controller;


import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lh
 * @date 2020/11/10  13:33
 */
public class HttpPost {
    public static void main(String[] args) {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Post请求
        URI uri = null;
        try {
            // 将参数放入键值对类NameValuePair中,再放入集合中
            //单个参数
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("activityCode", "UC_task_test01"));
            params.add(new BasicNameValuePair("giftNum", "1"));
            params.add(new BasicNameValuePair("language", "zh_CN"));
            params.add(new BasicNameValuePair("channelInfo", "GS,UserCenter"));
            params.add(new BasicNameValuePair("website", "zh_CN"));
            params.add(new BasicNameValuePair("senceCode", "OTHE"));
            // 设置uri信息,并将参数集合放入uri;
            // 注:这里也支持一个键值对一个键值对地往里面放setParameter(String key, String value)
           /* uri = new URIBuilder().setScheme("http").setHost("10.225.66.115").setPort(8080)
                    .setPath("/qpon/v1.3/present").setParameters(params).build();*/
            uri = new URIBuilder().setScheme("http").setHost("10.225.66.115").setPort(8080)
                    .setPath("/qpon/v1.3/present").build();
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        }

        org.apache.http.client.methods.HttpPost httpPost = new org.apache.http.client.methods.HttpPost(uri);
        // HttpPost httpPost = new
        // HttpPost("http://localhost:12345/doPostControllerThree1");
        // 创建参数
        // 将对象转换为json字符串，并放入entity中
        GrantDto grantDto = new GrantDto("UC_task_test01", "GS,UserCenter", "OTHE", "1000674969");
        //GrantDto grantDto = new GrantDto(activityCode, couponProperty.getChannelInfo(), couponProperty.getSenceCode(), memberId);
        //将对象转换为json字符串
        String requestJson = new Gson().toJson(grantDto);
        //String s = JSON.toJSONString(grantDto);
        StringEntity entitys = new StringEntity(requestJson, "UTF-8");
        httpPost.setHeader("Tenant-Id", "GS");
        httpPost.setHeader("Tenant-Secret", "9f9ca3f31d3703c79c90ca7b88c0462a");
        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        httpPost.setEntity(entitys);

        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //

    }
    static class GrantDto {
        /**
         *   "activityCode": "user_02",
         *   "giftNum": "1",
         *   "language": "zh_CN",
         *   "channelInfo": "GS,UserCenter",
         *   "website": "zh_CN",
         *   "memberInfo": {
         *     "memberId": "test"
         *   },
         *   "senceCode": "OTHE"
         */
        private String activityCode;
        private String giftNum = "1";
        private String language = "zh_CN";
        private String channelInfo;
        private String website = "zh_CN";
        private String senceCode;
        private MemberInfo memberInfo;

        GrantDto(String activityCode, String channelInfo, String senceCode, String memberId) {
            this.activityCode = activityCode;
            this.channelInfo = channelInfo;
            this.senceCode = senceCode;

            MemberInfo memberInfo = new MemberInfo(memberId);
            this.memberInfo = memberInfo;
        }
    }

    /**
     * 发券请求Dto中引用的对象
     */
    static class MemberInfo {
        private String memberId;

        MemberInfo(String memberId) {
            this.memberId = memberId;
        }
    }

}
